package com.hua.yuan.live.repertory.cloud_data.loader;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.AsyncTaskLoader;
import android.text.TextUtils;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.JsonResult;
import com.hua.yuan.live.repertory.cloud_data.api.LiveGetViewingApi;
import com.hua.yuan.live.repertory.cloud_data.api.LiveRoomInfoApi;
import com.hua.yuan.live.repertory.cloud_data.res.RoomInfoBean;

import java.io.IOException;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 主播详情页面
 */
public class LiveRoomInfoLoader extends AsyncTaskLoader<RoomInfoBean> {

    private String mToken;
    private int mAnchorId;
    private RoomInfoBean mTargetData;
    public LiveRoomInfoLoader(Context context) {
        super(context);
    }

    @Override
    public RoomInfoBean loadInBackground() {
        LiveRoomInfoApi roomInfoApi=  OkHttpModule.getInstance().getRetrofit().create(LiveRoomInfoApi.class);
        Call<String> call=roomInfoApi.getAnchorInfo(mToken,mAnchorId);
        try {
            Response<String> response = call.execute();
            String result=response.body();
            if(TextUtils.isEmpty(result)){
                return null;
            }
            JsonResult<JSONObject> resultData= JSONObject.parseObject(result,JsonResult.class);
            if(resultData==null){
                return null;
            }
            JSONObject data=resultData.getData();
            if(data==null){
                return null;
            }
            RoomInfoBean targetData = JSONObject.parseObject(data.toJSONString(),RoomInfoBean.class);
            if(targetData==null){
                return null;
            }
            if(mTargetData != null &&  targetData==mTargetData){
                return null;
            }
            //已开播时，显示访问人数
             if(targetData.getIsLive()==1){
                getViewing(targetData);
            }

            mTargetData=targetData;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mTargetData;
    }

    /**
     * 获取访问的人数
     */
    private void getViewing(@NonNull RoomInfoBean pInfoBean){
        LiveGetViewingApi liveGetViewingApi=  OkHttpModule.getInstance().getRetrofit().create(LiveGetViewingApi.class);
        Call<String> callViewing=liveGetViewingApi.getViewingNum(mToken,mAnchorId);
        try {
            Response<String> response = callViewing.execute();
            String result=response.body();
            if(TextUtils.isEmpty(result)){
                return;
            }
            JsonResult<Integer> resultData= JSONObject.parseObject(response.body(),JsonResult.class);
            if(resultData==null){
                return;
            }
            Integer number=resultData.getData();
            pInfoBean.setViewingNum(number);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 请求数据源
     * @param pToken
     * @param pAnchorId 主播的Id
     */
    public void requestData(String pToken,int pAnchorId){
        this.mToken=pToken;
        this.mAnchorId=pAnchorId;
    }


    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        if(mTargetData != null){
            //如果mData不需要刷新数据，那么则再次处理以往的旧数据
            deliverResult(mTargetData);
        }else{
            forceLoad();
        }
    }
}
